package com.example.cursor_orm.util;

import org.mindrot.jbcrypt.BCrypt;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Random;

public class CreateUser {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost:3306/rabc_db?useUnicode=true&characterEncoding=utf8";
        String user = "root";
        String password = "123456";
        Connection conn = DriverManager.getConnection(url, user, password);

        String[] depts = {"计算机系", "数学系", "物理系"};
        String[] grades = {"2021", "2022", "2023"};
        String[] userTypes = {"学生", "教师", "管理员"};
        Random rand = new Random();

        String sql = "INSERT INTO user (username, password_hash, salt, real_name, user_type, student_no, staff_no, dept, grade) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement ps = conn.prepareStatement(sql);

        for (int i = 1; i <= 10; i++) {
            String username = "yonghu" + i;
            String realName = "用户" + i;
            String userType = userTypes[rand.nextInt(userTypes.length)];
            String studentNo = userType.equals("学生") ? String.format("ST%04d", i) : null;
            String staffNo = !userType.equals("学生") ? String.format("SF%04d", i) : null;
            String dept = depts[rand.nextInt(depts.length)];
            String grade = userType.equals("学生") ? grades[rand.nextInt(grades.length)] : null;
            String plainPassword = "123456";
            String salt = BCrypt.gensalt();
            String passwordHash = BCrypt.hashpw(plainPassword, salt);

            ps.setString(1, username);
            ps.setString(2, passwordHash);
            ps.setString(3, salt);
            ps.setString(4, realName);
            ps.setString(5, userType);
            ps.setString(6, studentNo);
            ps.setString(7, staffNo);
            ps.setString(8, dept);
            ps.setString(9, grade);

            ps.executeUpdate();
        }

        ps.close();
        conn.close();
        System.out.println("10个用户已创建。");
    }
}